package com.ask.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.nacos.shaded.io.grpc.LoadBalancer;
import com.ask.order.bean.Order;
import com.ask.order.feign.ProductFeignClient;
import com.ask.order.service.IOrderService;
import com.ask.product.bean.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    private final String SERVICE_NAME = "service-product";

    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    @SentinelResource(value = "createOrder", blockHandler = "createOrderBlockHandler")
    public Order createOrder(Long productId, Long userId) {
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        order.setProductId(4L);
        order.setCount(100);
        order.setUserId(userId);
        order.setMoney(600);
        order.setProductList(Arrays.asList(product));
        return order;
    }

    public Order createOrderBlockHandler(Long productId, Long userId, BlockException e) {
        Order order = new Order();
        order.setId(0l);
        order.setProductId(0l);
        order.setCount(100);
        order.setUserId(userId);
        order.setMoney(0);
        order.setProductList(Arrays.asList(new Product()));
        return order;
    }

    private Product getProductFromRemote(Long productId) {
        // 获取到商品服务所在的所有机器IP+port
        List<ServiceInstance> instances = discoveryClient.getInstances(SERVICE_NAME);
        ServiceInstance instance = instances.get(0);
        // 远程url
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        // 发送远程请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    private Product getProductFromRemoteWithLoadBalance(Long productId) {
        // 获取到商品服务所在的所有机器IP+port
        ServiceInstance instance = loadBalancerClient.choose(SERVICE_NAME);

        // 远程url
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        // 发生远程请求
        Product product = restTemplate.getForObject(url, Product.class);
        product.setInstanceInfo(url);
        return product;
    }

    private Product getProductFromRemoteWithLoadBalance3(Long productId) {
        // 远程url
        String url = "http://" + SERVICE_NAME + "/product/" + productId;
        // 发送远程请求
        Product product = restTemplate.getForObject(url, Product.class);
        product.setInstanceInfo(url);
        return product;
    }
}
